package lisen.stes.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.json.simple.parser.JSONParser;

import lisen.stes.entity.User;
import lisen.stes.util.NameStore;

/**
 * Servlet implementation class for Servlet: MemberServlet
 *
 */
public class MemberServlet extends BaseServlet {
	static final long serialVersionUID = 1L;

	@Override
	protected void execute(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
//		req.setAttribute("members", NameStore.users);
		req.getRequestDispatcher("members.jsp").forward(req, resp);
	}
	
	
	protected void query(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException {
		String keyword  = req.getParameter("keyword");
		List<User> users = NameStore.query(keyword);
		System.out.println(users);
		JSONArray.writeJSONString(users, resp.getWriter() );
		//resp.getWriter().write(users.toString());
		//req.setAttribute("members", NameStore.query(keyword));
		//req.getRequestDispatcher("members.jsp").forward(req, resp);
	}
	
	protected void allMembers(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException {
		resp.getWriter().write(NameStore.users.toString());
		//resp.getWriter().write(users.toString());
		//req.setAttribute("members", NameStore.query(keyword));
		//req.getRequestDispatcher("members.jsp").forward(req, resp);
	}	
}